package yicr.untils;


import org.apache.poi.ss.usermodel.*;
import yicr.model.excel.ExcelOutputTemplate;
import yicr.model.excel.ExcelStringRowTable;
import yicr.model.excel.FillTemplateExcelGroupRowsHandler;

import java.io.*;
import java.util.List;

public class NslUtilExcelFillOutputTemplate {


    public  <T>  void demo1(){

    }

    /**
     * 以 dataList 中t元素，填充模板
     * 每个 t 元素填充一组模板，填充组数为 datalist.size()
     *
     * @param excelOutputTemplate
     * @param dataList
     * @param outputFilePath 输出目录
     * @param <T>
     */
    public static <T>  void fillOutputTemplate(ExcelOutputTemplate excelOutputTemplate, List<T> dataList, String outputFilePath, FillTemplateExcelGroupRowsHandler fillHandler){
        try (InputStream inp = new FileInputStream(excelOutputTemplate.getFileAbsPath())) {
//InputStream inp = new FileInputStream("workbook.xlsx");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheet(excelOutputTemplate.getSheetName());

            int beginRowIndex= excelOutputTemplate.getDataGroupBeginRowIndex();
            int endRowIndex= excelOutputTemplate.getDataGroupEndRowIndex();
            List<List<Row>> rowGroupList = NslUtilExcelCopyUtils.copyGroupRowAndReturnGroupList(sheet,beginRowIndex,endRowIndex,dataList.size());

            //以group row为单位填充
            for (int i = 0; i < rowGroupList.size(); i++) {
                T data=dataList.get(i);
                List<Row> rowGroup=rowGroupList.get(i);
                fillHandler.fillGroupRows(data,rowGroup);
            }

            //公式重新计算
            NslUtilExcelFormula.calculateFormulaOfSheet(sheet);


            try (OutputStream fileOut = new FileOutputStream(outputFilePath)) {
                wb.write(fileOut);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 将 excelStringRowTable 写入 sheet中，表头行数为 headerCount,
     * @param excelStringRowTable
     * @param sheet
     * @param headerCount
     *
     */
    public static void fillOutputTemplateWithStringRowTable(ExcelStringRowTable excelStringRowTable, Sheet sheet, int headerCount){
        List<ExcelStringRowTable.ExcelStringRow> rowList=excelStringRowTable.getRowList();
        int rowIndex=headerCount;
        for (ExcelStringRowTable.ExcelStringRow excelStringRow : rowList) {
            Row currentRow=sheet.getRow(rowIndex);
            if(currentRow==null){
                currentRow=sheet.createRow(rowIndex);
            }
            List<String> valueList=excelStringRow.getStringList();
            for (int i = 0; i < valueList.size(); i++) {
                Cell cell=currentRow.getCell(i);
                if(cell==null){
                    cell=currentRow.createCell(i);
                }
                cell.setCellValue(valueList.get(i));
            }
            rowIndex++;
        }
    }
}
